//管理用户数据相关
//登录即登录用户信息
import { defineStore } from 'pinia'
import { loginAPI } from "@/apis/user";
import { ref } from 'vue';
import { useCartStore } from "@/stores/cartStore";
import { mergeCartAPI } from '@/apis/cart'
export const useUserStore = defineStore('user', () => {
    const cartStore = useCartStore()
    //用户数据state
    const userInfo = ref({})
    //登录
    const getUserInfo = async ({ account, password }) => {
        const res = await loginAPI({ account, password })
        userInfo.value = res.result
        await mergeCartAPI(cartStore.cartList.map(item => {
            return {
                skuId: item.skuId,
                selected: item.selected,
                count: item.count
            }
        }))
        cartStore.updateNewList()
    }

    //退出登录，清除信息
    const clearUserInfo = () => {
        userInfo.value = {}
        //清除购物车
        cartStore.clearCart()
    }
    return {
        userInfo,
        getUserInfo,
        clearUserInfo
    }
},
    {
        persist: true
    })